package com.atguigu.lease.web.app.service.impl;

import com.atguigu.lease.common.exceptions.LeaseException;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.model.entity.ApartmentInfo;
import com.atguigu.lease.model.entity.FacilityInfo;
import com.atguigu.lease.model.entity.LabelInfo;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.app.mapper.*;
import com.atguigu.lease.web.app.service.ApartmentInfoService;
import com.atguigu.lease.web.app.vo.apartment.ApartmentDetailVo;
import com.atguigu.lease.web.app.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.app.vo.graph.GraphVo;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【apartment_info(公寓信息表)】的数据库操作Service实现
 * @createDate 2023-07-26 11:12:39
 */
@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo>
        implements ApartmentInfoService {

    //图片
    @Autowired
    private GraphInfoMapper graphInfoMapper;

    //标签
    @Autowired
    private LabelInfoMapper labelInfoMapper;
    //配套
    @Autowired
    private FacilityInfoMapper facilityInfoMapper;
    //租金
    @Autowired
    private RoomInfoMapper roomInfoMapper;
    //公寓
    @Autowired
    private  ApartmentInfoMapper apartmentInfoMapper;

    @Override
    public ApartmentDetailVo selectApartmentDetailVoById(Long id) {
        //公寓
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectById(id);
        if (apartmentInfo == null){
            throw new LeaseException(ResultCodeEnum.DATA_ERROR);
        }
        //图片
        List<GraphVo> graphVoList =  graphInfoMapper.selectGraphInfoByItemTypeAndId(ItemType.APARTMENT,id);
        //标签
        List<LabelInfo> labelInfoList = labelInfoMapper.selectLabelInfoByApartmentId(id);
        //配套
        List<FacilityInfo> facilityInfoList = facilityInfoMapper.selectFacilityInfoByAprtmentId(id);
        //最低房租
        BigDecimal minRent = roomInfoMapper.selectMinRentByApartmentId(id);

        //装配数据--拼接
        ApartmentDetailVo apartmentDetailVo = new ApartmentDetailVo();
        BeanUtils.copyProperties(apartmentInfo,apartmentDetailVo);
        apartmentDetailVo.setFacilityInfoList(facilityInfoList);
        apartmentDetailVo.setGraphVoList(graphVoList);
        apartmentDetailVo.setLabelInfoList(labelInfoList);
        apartmentDetailVo.setMinRent(minRent);
        apartmentDetailVo.setIsDelete(apartmentInfo.getIsDeleted() == 1);
        return apartmentDetailVo;
    }

    @Override
    public ApartmentItemVo selectApartmentItemVoById(Long id) {
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectApartmentById(id);

        //图片
        List<GraphVo> graphVoList =  graphInfoMapper.selectGraphInfoByItemTypeAndId(ItemType.APARTMENT,id);
        //标签
        List<LabelInfo> labelInfoList = labelInfoMapper.selectLabelInfoByApartmentId(id);
        //最低房租
        BigDecimal minRent = roomInfoMapper.selectMinRentByApartmentId(id);

        ApartmentItemVo apartmentItemVo = new ApartmentItemVo();

        BeanUtils.copyProperties(apartmentInfo, apartmentItemVo);
        apartmentItemVo.setGraphVoList(graphVoList);
        apartmentItemVo.setLabelInfoList(labelInfoList);
        apartmentItemVo.setMinRent(minRent);

        return apartmentItemVo;
    }

}




